package com.example.myapp.APP;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.ADO.ActivityManager;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.Money;
import com.example.myapp.Model.MyData;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Jizhangben extends AppCompatActivity {
   private ListView listView;
   private TextView summoney;
   private TextView addmoney;
     private   TextView outmoney;
   private ImageView jizhangback;
  private   Button xinjian;
  private   Double  sum=0.0;
  private   Double shouru=0.0;
    private MyData myData;
    private ShapeLoadingDialog shapeLoadingDialog;
    private  List<Money> data = new ArrayList<>();
    public void onBackPressed() {
        //此处写退向后台的处理
        finish();
    }//手机返回键
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1: {

                    for (int i=0;i<data.size();i++){
                        sum+=data.get(i).getMoney();
                        if (data.get(i).getMoney()>0){
                            shouru+=data.get(i).getMoney();
                        }
                    }
                    summoney.setText(""+sum);
                    addmoney.setText(""+shouru);
                    outmoney.setText(""+(sum-shouru));
                    Adapter adapter = new Adapter(Jizhangben.this,data);//NewAdapterDAO为自定义适配器
                    listView.setAdapter(adapter);
                    break;
                }
            }
        }
    };





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "33854715fa739da69a015d5ab944d101");

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.jizhang);
        listView=findViewById(R.id.zhangdan);
        summoney=findViewById(R.id.summoney);
        addmoney=findViewById(R.id.addmoney);
        outmoney=findViewById(R.id.outmoney);
        jizhangback=findViewById(R.id.jizhangback);
        xinjian=findViewById(R.id.xinjian);
        myData=(MyData)getApplication();
        if (myData.getMoneyList().size()==0){
            Log.w("aaaa","数据为空");
        querymonry();
        }else {
            Log.w("aaaa","数据不为空");
            data=myData.getMoneyList();
            Message message = new Message();
            message.what = 1;
            // message.obj = s;//得到是本地图片地址，将地址已消息的方式发送给线程
            handler.sendMessage(message);
        }

        xinjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Jizhangben.this, Zhichu.class);
                startActivity(intent);
                finish();
            }
        });

        jizhangback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });

    }

    //查询
    private void querymonry(){
        openLoading();
            BmobQuery<Money> query = new BmobQuery<Money>();
            User user = BmobUser.getCurrentUser(User.class);//查询自己
            //friend是User表中的字段，用来存储所有关注的用户
           query.addWhereEqualTo("author",user.getObjectId());
            //包含作者信息
        query.include("user,author");
          query.sum(new String[]{"money"});
             query.order("-updatedAt");
            query.findObjects(new FindListener<Money>() {
                @Override
                public void done(List<Money> object, BmobException e) {
                    if (e == null) {
                        data=object;
                        myData.setMoneyList(object);
                        Message message = new Message();
                        message.what = 1;
                        // message.obj = s;//得到是本地图片地址，将地址已消息的方式发送给线程
                        handler.sendMessage(message);

                        Log.w("aaaa", "查询记账表成功！"+object.size());
                        closeLoading();
                    } else {
                        Log.e("aaaa","失败"+ e.getMessage());

                    }
                }

            });

    }
    //网络加载
    private  void openLoading(){
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(Jizhangben.this)
                .loadText("加载中...")
                .build();
        shapeLoadingDialog.show();
    }
    //关闭网络加载
    private  void closeLoading(){
        shapeLoadingDialog.dismiss();
    }

    private class Adapter extends BaseAdapter{
        private Context context;
        private  List<Money> moneyList = new ArrayList<>();
        private LayoutInflater mInflater;
        public Adapter(Context context,List<Money> moneyList) {
            this.context = context;
            this.moneyList = moneyList;

            mInflater = LayoutInflater.from(context);//初始化
        }
        @Override
        public boolean areAllItemsEnabled() {
            return super.areAllItemsEnabled();
        }
        @Override
        public int getCount() {
            return moneyList.size();
        }

        @Override
        public Object getItem(int i) {
            return moneyList.get(i);
        }
        @Override
        public long getItemId(int i) {
            return i;
        }


        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = mInflater.inflate(R.layout.listzhangdan, null);
            TextView listmoney = view.findViewById(R.id.listmoney);
            TextView zhangdantime = view.findViewById(R.id.zhangdantime);
            TextView zhangdanclass = view.findViewById(R.id.zhangdanclass);
            TextView zhangdanremarks = view.findViewById(R.id.zhangdanremarks);
             ImageView zhangdanicon = view.findViewById(R.id.zhangdanicon);

            if (moneyList.get(i).getMoney()>0){
                listmoney.setTextColor(Color.parseColor("#FFBC34"));
                listmoney.setText("+"+moneyList.get(i).getMoney());
            }else {
                listmoney.setText(""+moneyList.get(i).getMoney());
            }
            zhangdantime.setText(moneyList.get(i).getUpdatedAt());
            zhangdanclass.setText(moneyList.get(i).getClassify());
            zhangdanremarks.setText(moneyList.get(i).getRemarks());

           if(moneyList.get(i).getClassify().equals("餐饮")){
               zhangdanicon.setImageResource(R.drawable.canyin);
           }else if(moneyList.get(i).getClassify().equals("服饰")){
               zhangdanicon.setImageResource(R.drawable.fushi);
           }else if(moneyList.get(i).getClassify().equals("水电")){
               zhangdanicon.setImageResource(R.drawable.shuidian);
           }else if(moneyList.get(i).getClassify().equals("住房")){
               zhangdanicon.setImageResource(R.drawable.zhufang);
           }else if(moneyList.get(i).getClassify().equals("出行")){
               zhangdanicon.setImageResource(R.drawable.chuxing);
           }else if(moneyList.get(i).getClassify().equals("转账")){
               zhangdanicon.setImageResource(R.drawable.zhuanzhang);
           } else if(moneyList.get(i).getClassify().equals("工资")){
            zhangdanicon.setImageResource(R.drawable.gongzi);
           }else if(moneyList.get(i).getClassify().equals("补贴")){
               zhangdanicon.setImageResource(R.drawable.butie);
           }else if(moneyList.get(i).getClassify().equals("奖金")){
               zhangdanicon.setImageResource(R.drawable.jiangjin);
           }

           else {
               zhangdanicon.setImageResource(R.drawable.qita);
           }

            return view;
        }
    }












}
